const folder = {
   project: 'test',
   dist: 'dist',
   src: 'src',
   html: '',
   css: '',
   js: '',
   pug: '',
   stylus: '',
   coffee: ''
}

const file = {
   html: 'index',
   css: '_',
   js: '_',
   pug: '_',
   stylus: '_',
   coffee: '_'
}



















pathbuild = function(a) {
   if (typeof a === 'object') {
      return a.filter(function (el) {return (el != '')}).join('/')
   } else { return '' }
}

const propath = {
   src: pathbuild(['..', folder.project, folder.src]),
   dist: pathbuild(['..', folder.project, folder.dist])
}

const path = {
   server: pathbuild(['.', propath.dist, folder.html]),
   html: pathbuild([propath.dist, folder.html]),
   css: pathbuild([propath.dist, folder.css]),
   js: pathbuild([propath.dist, folder.js]),
   pug: pathbuild([propath.src, folder.pug]),
   stylus: pathbuild([propath.src, folder.stylus]),
   coffee: pathbuild([propath.src, folder.coffee]),
   file: {
      pug: pathbuild([propath.src, folder.pug, file.pug + '.pug']),
      stylus: pathbuild([propath.src, folder.stylus, file.stylus + '.styl']),
      coffee: pathbuild([propath.src, folder.coffee, file.coffee + '.coffee'])
   },
   watch: {
      pug: pathbuild([propath.src, folder.pug, '**/*.pug']),
      stylus: pathbuild([propath.src, folder.stylus, '**/*.styl']),
      coffee: pathbuild([propath.src, folder.coffee, '**/*.coffee'])
   }
}

const gulp = require('gulp')
const stylus = require('gulp-stylus')
const notify = require('gulp-notify')
const browsersync = require('browser-sync').create()
const autoprefixer = require('gulp-autoprefixer')
const pug = require('gulp-pug')
const rename = require('gulp-rename')
const coffee = require('gulp-coffee')


function sync_(done) {
    browsersync.init({
        server: {baseDir: path.server},
        port: 3000
    });
    done()
}

function stylus_() {
   return gulp.src(path.file.stylus)
      .pipe(stylus())
      .on("error", notify.onError({
         message: "<%= error.message %>",
         title: "Stylus error!"
      }))
      .pipe(rename({ basename: file.css}))
      .pipe(autoprefixer())
      .pipe(gulp.dest(path.css))
      .pipe(notify('Stylus complete :)'))
      .pipe(browsersync.stream())
}

function pug_() {
   return gulp.src(path.file.pug)
      .pipe(pug())
      .on("error", notify.onError({
         message: "<%= error.message %>",
         title: "Pug error!"
      }))
      .pipe(rename({ basename: file.html}))
      .pipe(gulp.dest(path.html))
      .pipe(notify('Pug complete :)'))
      .pipe(browsersync.stream())
}

function coffee_() {
   return gulp.src(path.file.coffee)
      .pipe(coffee())
      .on("error", notify.onError({
         message: "<%= error.message %>",
         title: "Coffee error!"
      }))
      .pipe(rename({ basename: file.coffee}))
      .pipe(gulp.dest(path.js))
      .pipe(notify('Coffee complete :)'))
      .pipe(browsersync.stream())
}

function watch_() {
   gulp.watch(path.watch.stylus, stylus_)
   gulp.watch(path.watch.pug, pug_)
   gulp.watch(path.watch.coffee, coffee_)
}

exports.stylus = stylus_
exports.watch = watch_
exports.pug = pug_
exports.coffee = coffee_

exports.default = gulp.series(stylus_, coffee_, pug_, sync_, watch_)

